﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace TopShooter
{
    public partial class Game
    {
        public class State
        {

            public string CurrentSceneName;

            public string StaticContent;

            public List<SceneContentEntry> SceneContentList;

            public State(Game game)
            {
                CurrentSceneName = game.Scene.Name;
                StaticContent = game.Scene.GetStaticContent();

                SceneContentList = new List<SceneContentEntry>(Scene.DynamicContentRegistry.Count);

                foreach (var pair in Scene.DynamicContentRegistry)
                {
                    SceneContentEntry entry = new SceneContentEntry() { SceneName = pair.Key, Content = pair.Value };
                    SceneContentList.Add(entry);
                }
            }

            private State()
            {
            }

            public void Save()
            {
                XmlSerializer ser = new XmlSerializer(typeof(State));
                System.IO.FileStream stream = new System.IO.FileStream("testState.xml", System.IO.FileMode.Create);
                ser.Serialize(stream, this);

                stream.Close();
            }
            static public State Load()
            {
                XmlSerializer ser = new XmlSerializer(typeof(State));
                System.IO.FileStream stream = new System.IO.FileStream("testState.xml", System.IO.FileMode.Open);

                State s = ser.Deserialize(stream) as State;

                stream.Close();

                return s;
            }
            public void Apply(Game game)
            {
                Scene.DynamicContentRegistry.Clear();
                foreach (var entry in SceneContentList)
                {
                    Scene.DynamicContentRegistry.Add(entry.SceneName, entry.Content);
                }

                Portal.PortalTarget pd = new Portal.PortalTarget(CurrentSceneName + ",0,W");

                Scene.Manager.Instance.LoadSceneByName(CurrentSceneName, pd);

                // Next Task -- apply the saved scene content...

            }


            public class SceneContentEntry
            {
                public string SceneName;
                public List<Info> Content;
            }

        }
    }
}
